package com.fengkai.filepost.pdfoutstream.test.ODsolutions;

import org.junit.Test;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author Fengkai
 * @creat 2023/3/29
 */
public class CpuQuartz {

    public static int getResult(int qps, int size, int [] quartz){
        //每秒执行qps个任务  任务队列长size  数组表示每秒进来多少个任务 每个任务执行都是1秒
        //执行不完就缓存到下一个队列里
        int time = 0, cache = 0;
        for (int a: quartz) {
            if (qps >= a + cache){
                cache = 0;
            }else {
                cache += a - qps;

            }
            time ++;
        }
        if (cache != 0){
            time += cache/qps + (cache % qps == 0 ? 0 : 1 );
        }
        return time;
    }

    @Test
    public static  int test(int qps, int size, int [] quartz) {
            int needTime = 0, leave = 0;
            for (int count : quartz) {
                if (qps < count + leave) {
                    leave = count + leave - qps;
                } else {
                    leave = 0;
                }
                ++needTime;
            }
            needTime += leave / qps;
            if (0 < leave % qps) {
                ++needTime;
            }
            return needTime;

    }


       public static void main(String[] args){
           for (int i = 0; i < 1000; i++) {
               int v = (int)(Math.random() * 10000 + 1);
               int size = (int)(Math.random() * 10000 + 1);
               int[] ints = new int[size];
               for (int j  = 0; j < size; j++) {
                   ints[j] = (int)(Math.random() * 10000 + 1);
               }
               if (!(getResult(v, size, ints) == test(v, size, ints))){
                   System.out.println("出错啦");
               }
           }



       }
}
